package qz_rpt

class ScrapSummaryController {

    def commonDataService
    def dynamicQueryService
    def exportFileService

    def index(Integer max) {
        def cellList = org.kdx.DataUtils.getCellList()
        def pageSizeList = org.kdx.DataUtils.getPageSizeList()

        [totalCount: 5, pageSize: 20, pageCurrent: 1, orderField: 'id', orderDirection: 'asc', pageSizeList: pageSizeList, cellList: cellList]
    }

    def scrapSummary(){
        request.setCharacterEncoding("UTF-8")
        response.setContentType("text/html;charset=UTF-8")

        params.scrapAttribute = params.scrapAttribute?Arrays.asList(params.scrapAttribute):(params['scrapAttribute[]']?Arrays.asList(params['scrapAttribute[]']):'')
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 100
        params.startno = ((pageCurrent - 1) * pageSize) + 1
        params.endno = pageCurrent * pageSize
        def scrapSummarySum=[]
        def scrapSummary=[]
        def pageSizeList = [10,20,50,100]
        def totalCount =0
        def tableHeadList = []
        params.SYS_QUERY_NAME = 'scrapSummaryDepartment'
        def scrapSummaryDepartment = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'scrapSummaryStep'
        def scrapSummaryStep = dynamicQueryService.queryForList(params)
        /*params.SYS_QUERY_NAME = 'scrapSummaryUser'
        def scrapSummaryUser = dynamicQueryService.queryForList(params)*/
        params.SYS_QUERY_NAME = 'scrapSummaryScrapAttribute'
        def scrapSummaryScrapAttribute = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'scrapSummaryReAdd'
        def scrapSummaryReAdd = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'orderStatusAnalysisStartedList'
        def orderStatusAnalysisStartedList = dynamicQueryService.queryForList(params)
        if(params.EXPORT_CONTENT_NAME=='Excel'){
            params.SYS_QUERY_NAME = 'scrapSummary'
            scrapSummary = dynamicQueryService.queryForList(params)

            if (scrapSummary.size() > 0) {
                tableHeadList = scrapSummary[0].keySet()
            }
            params.SYS_QUERY_NAME = 'scrapSummaryCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        }
        /*params.SYS_QUERY_NAME = 'scrapSummary'
        scrapSummary = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (scrapSummary.size() > 0) {
            tableHeadList = scrapSummary[0].keySet()
        }
        params.SYS_QUERY_NAME = 'scrapSummaryCount'
        totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT*/

       /* params.SYS_QUERY_NAME = 'scrapSummaryPartName'
        def scrapSummaryPartName = dynamicQueryService.queryForList(params)*/

        /*params.SYS_QUERY_NAME = 'scrapSummaryLineId'
        def scrapSummaryLineId = dynamicQueryService.queryForList(params)*/
        params.SYS_QUERY_NAME = 'scrapSummarySumQty'
        def scrapSummarySumQty = dynamicQueryService.queryForList(params)
        println("12321321321")

        [pageSize  : pageSize, pageSizeList: pageSizeList, startno: params.startno ?: '', endno: params.endno ?: '',
         totalCount: totalCount, tableHeadList: tableHeadList, scrapSummary: scrapSummary,
          startTime: params.startTime ?: '', endTime: params.endTime ?: '',
        partName:params.partName?:'', lineId:params.lineId?:'',
         scrapSummaryDepartment:scrapSummaryDepartment,department:params.department?:'',
         scrapSummaryStep:scrapSummaryStep,step:params.step?:'', user:params.user?:'',
         scrapSummaryScrapAttribute:scrapSummaryScrapAttribute,scrapAttribute:params.scrapAttribute?:'',orderStatusAnalysisStartedList:orderStatusAnalysisStartedList,
         scrapSummaryReAdd:scrapSummaryReAdd,readd:params.readd?:'',scrapSummarySumQty:scrapSummarySumQty,startedState:params.startedState?:'']
    }

    def exportExcel() {
        def sysQueryName


        sysQueryName = params.SYS_QUERY_NAME

        params.startno = 1
        params.endno = 99999999999999999



        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        def tableHeadList =  []
        if(dataList.size()>0){

                tableHeadList = dataList[0].keySet()

        }

print(tableHeadList)

String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
response.setContentType('application/vnd.ms-excel;charset=UTF-8')
response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
response.setCharacterEncoding("UTF-8");
render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList,tableHeadList:tableHeadList,startno:params.startno?:'',endno:params.endno?:''])
}
}